package gui;

import gui.utils.UnsubscribeComboBoxModel;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JComboBox;

public class PanelPublish extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;
	private javax.swing.JButton buttonPublish;
	private javax.swing.JComboBox comboBoxTopicsList;
	private javax.swing.JLabel labelDescriptionMessage;
	private javax.swing.JLabel labelDescriptionTopicsList;
	private javax.swing.JTextField textFieldMessage;

	public PanelPublish() {
		initComponents();
	}

	private void buttonPublishActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			ClientGuiLauncher.clientFrame.client
					.publish(textFieldMessage.getText(), comboBoxTopicsList
							.getSelectedItem().toString());
		} catch (Exception e) {
			Logger.getLogger(PanelPublish.class.getName()).log(Level.SEVERE,
					null, e);
		}
	}

	private void comboBoxTopicsListActionPerformed(
			java.awt.event.ActionEvent evt) {
		if (comboBoxTopicsList.getSelectedItem().equals(""))
			buttonPublish.setEnabled(false);
		else if (!textFieldMessage.getText().equals(""))
			buttonPublish.setEnabled(true);
	}

	protected void disableAll() {
		textFieldMessage.setEditable(false);
		textFieldMessage.setText("");
		comboBoxTopicsList.setModel(new UnsubscribeComboBoxModel(
				ClientGuiLauncher.clientFrame.client.getSubscribedTopics()));
		buttonPublish.setEnabled(false);
	}

	public JComboBox getComboBoxTopicsList() {
		return comboBoxTopicsList;
	}

	private void initComponents() {

		labelDescriptionTopicsList = new javax.swing.JLabel();
		labelDescriptionMessage = new javax.swing.JLabel();
		comboBoxTopicsList = new javax.swing.JComboBox();
		textFieldMessage = new javax.swing.JTextField();
		buttonPublish = new javax.swing.JButton();

		labelDescriptionTopicsList
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		labelDescriptionTopicsList
				.setText("Select the topic to which you want to publish");

		labelDescriptionMessage
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		labelDescriptionMessage.setText("Insert message");

		comboBoxTopicsList.setEditable(false);
		comboBoxTopicsList
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						comboBoxTopicsListActionPerformed(evt);
					}
				});

		textFieldMessage.setEditable(false);
		textFieldMessage.addKeyListener(new java.awt.event.KeyAdapter() {
			@Override
			public void keyReleased(java.awt.event.KeyEvent evt) {
				textFieldMessageKeyReleased(evt);
			}
		});

		buttonPublish.setText("Publish");
		buttonPublish.setEnabled(false);
		buttonPublish.setMargin(new java.awt.Insets(2, 5, 2, 5));
		buttonPublish.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonPublishActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												false)
												.addComponent(
														comboBoxTopicsList,
														javax.swing.GroupLayout.Alignment.LEADING,
														0,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														labelDescriptionTopicsList,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														labelDescriptionMessage,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														311, Short.MAX_VALUE)
												.addComponent(
														textFieldMessage,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														311, Short.MAX_VALUE))
								.addGap(18, 18, 18)
								.addComponent(buttonPublish,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										80,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														labelDescriptionTopicsList)
												.addComponent(
														labelDescriptionMessage))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														comboBoxTopicsList,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														20,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														textFieldMessage,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														buttonPublish,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														20,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
	}

	protected void setInitialComponentsState() {
		textFieldMessage.setEditable(true);
		textFieldMessage.setText("");
		buttonPublish.setEnabled(false);
		comboBoxTopicsList.setEditable(true);
		comboBoxTopicsList.setModel(new UnsubscribeComboBoxModel(
				ClientGuiLauncher.clientFrame.client.getSubscribedTopics()));
	}

	private void textFieldMessageKeyReleased(java.awt.event.KeyEvent evt) {
		if (textFieldMessage.getText().equals(""))
			buttonPublish.setEnabled(false);
		else if (!comboBoxTopicsList.getSelectedItem().equals(""))
			buttonPublish.setEnabled(true);
	}
}
